package com.sui.bigdata.rtcadmin.exception;

import lombok.Data;
import org.springframework.http.HttpStatus;

import java.util.Arrays;
import java.util.List;

/**
 * @author YongChen
 * @date 2019/11/6 14:41
 * @description
 * @email yong_chen@sui.com
 */
@Data
public class ApiError {

    private HttpStatus status;
    private String message;
    private String developerInfo;
    private List<ErrorItem> errors;

    public ApiError(final HttpStatus status, final String message) {
        this.status = status;
        this.message = message;
    }

    public ApiError(final HttpStatus status, final String message, final String developerInfo) {
        this.status = status;
        this.message = message;
        this.developerInfo = developerInfo;
    }

    public ApiError(final HttpStatus status, final String message, final List<ErrorItem> errors) {
        this.status = status;
        this.message = message;
        this.errors = errors;
    }

    public ApiError(final HttpStatus status, final String message, final ErrorItem error) {
        this.status = status;
        this.message = message;
        errors = Arrays.asList(error);
    }

    public void setError(final ErrorItem error) {
        errors = Arrays.asList(error);
    }

    @Data
    public static class ErrorItem {
        String field;
        String message;

        public ErrorItem(String field, String message) {
            this.field = field;
            this.message = message;
        }
    }

    public static ErrorItem makeErrorItem(String field, String message) {
        return new ErrorItem(field, message);
    }

}
